<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备ID" prop="assetId">
        <el-input
          v-model="queryParams.assetId"
          placeholder="请输入设备ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入计划标题"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划维护日期" prop="planDate">
        <el-date-picker clearable
          v-model="queryParams.planDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择计划维护日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="是否需要备件：0-否 1-是" prop="needSpare">
        <el-input
          v-model="queryParams.needSpare"
          placeholder="请输入是否需要备件：0-否 1-是"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划维护工种代码" prop="tradeCode">
        <el-input
          v-model="queryParams.tradeCode"
          placeholder="请输入计划维护工种代码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="允许开始窗口" prop="windowStart">
        <el-date-picker clearable
          v-model="queryParams.windowStart"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择允许开始窗口">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="允许结束窗口" prop="windowEnd">
        <el-date-picker clearable
          v-model="queryParams.windowEnd"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择允许结束窗口">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="预估工时" prop="estDurationMin">
        <el-input
          v-model="queryParams.estDurationMin"
          placeholder="请输入预估工时"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="目标班组" prop="assigneeGroup">
        <el-input
          v-model="queryParams.assigneeGroup"
          placeholder="请输入目标班组"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预指派负责人" prop="assigneeUserId">
        <el-input
          v-model="queryParams.assigneeUserId"
          placeholder="请输入预指派负责人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="周期数值，如每3月一次=&gt;MONTH,3" prop="cycleValue">
        <el-input
          v-model="queryParams.cycleValue"
          placeholder="请输入周期数值，如每3月一次=&gt;MONTH,3"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="系统计算的下次计划日期" prop="nextPlanDate">
        <el-date-picker clearable
          v-model="queryParams.nextPlanDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择系统计算的下次计划日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="创建人" prop="createdBy">
        <el-input
          v-model="queryParams.createdBy"
          placeholder="请输入创建人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="逻辑删除：0-否 1-是" prop="deleted">
        <el-input
          v-model="queryParams.deleted"
          placeholder="请输入逻辑删除：0-否 1-是"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="tradeCodeNorm">
        <el-input
          v-model="queryParams.tradeCodeNorm"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['sbtz:mtplan:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['sbtz:mtplan:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sbtz:mtplan:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['sbtz:mtplan:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="mtplanList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="设备ID" align="center" prop="assetId" />
      <el-table-column label="计划标题" align="center" prop="title" />
      <el-table-column label="计划维护日期" align="center" prop="planDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.planDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划维护内容" align="center" prop="planContent" />
      <el-table-column label="是否需要备件：0-否 1-是" align="center" prop="needSpare" />
      <el-table-column label="计划维护工种代码" align="center" prop="tradeCode" />
      <el-table-column label="优先级" align="center" prop="priority" />
      <el-table-column label="计划类型：保养/点检/修复" align="center" prop="planType" />
      <el-table-column label="允许开始窗口" align="center" prop="windowStart" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.windowStart, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="允许结束窗口" align="center" prop="windowEnd" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.windowEnd, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预估工时" align="center" prop="estDurationMin" />
      <el-table-column label="目标班组" align="center" prop="assigneeGroup" />
      <el-table-column label="预指派负责人" align="center" prop="assigneeUserId" />
      <el-table-column label="周期类型" align="center" prop="cycleType" />
      <el-table-column label="周期数值，如每3月一次=&gt;MONTH,3" align="center" prop="cycleValue" />
      <el-table-column label="系统计算的下次计划日期" align="center" prop="nextPlanDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.nextPlanDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="到期生成工单策略：手动/自动" align="center" prop="genPolicy" />
      <el-table-column label="计划状态" align="center" prop="status" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建人" align="center" prop="createdBy" />
      <el-table-column label="逻辑删除：0-否 1-是" align="center" prop="deleted" />
      <el-table-column label="${comment}" align="center" prop="tradeCodeNorm" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['sbtz:mtplan:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['sbtz:mtplan:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备计划维护对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="mtplanRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备ID" prop="assetId">
          <el-input v-model="form.assetId" placeholder="请输入设备ID" />
        </el-form-item>
        <el-form-item label="计划标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入计划标题" />
        </el-form-item>
        <el-form-item label="计划维护日期" prop="planDate">
          <el-date-picker clearable
            v-model="form.planDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择计划维护日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="计划维护内容">
          <editor v-model="form.planContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="是否需要备件：0-否 1-是" prop="needSpare">
          <el-input v-model="form.needSpare" placeholder="请输入是否需要备件：0-否 1-是" />
        </el-form-item>
        <el-form-item label="计划维护工种代码" prop="tradeCode">
          <el-input v-model="form.tradeCode" placeholder="请输入计划维护工种代码" />
        </el-form-item>
        <el-form-item label="允许开始窗口" prop="windowStart">
          <el-date-picker clearable
            v-model="form.windowStart"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择允许开始窗口">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="允许结束窗口" prop="windowEnd">
          <el-date-picker clearable
            v-model="form.windowEnd"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择允许结束窗口">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="预估工时" prop="estDurationMin">
          <el-input v-model="form.estDurationMin" placeholder="请输入预估工时" />
        </el-form-item>
        <el-form-item label="目标班组" prop="assigneeGroup">
          <el-input v-model="form.assigneeGroup" placeholder="请输入目标班组" />
        </el-form-item>
        <el-form-item label="预指派负责人" prop="assigneeUserId">
          <el-input v-model="form.assigneeUserId" placeholder="请输入预指派负责人" />
        </el-form-item>
        <el-form-item label="周期数值，如每3月一次=&gt;MONTH,3" prop="cycleValue">
          <el-input v-model="form.cycleValue" placeholder="请输入周期数值，如每3月一次=&gt;MONTH,3" />
        </el-form-item>
        <el-form-item label="系统计算的下次计划日期" prop="nextPlanDate">
          <el-date-picker clearable
            v-model="form.nextPlanDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择系统计算的下次计划日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="创建人" prop="createdBy">
          <el-input v-model="form.createdBy" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="逻辑删除：0-否 1-是" prop="deleted">
          <el-input v-model="form.deleted" placeholder="请输入逻辑删除：0-否 1-是" />
        </el-form-item>
        <el-form-item label="${comment}" prop="tradeCodeNorm">
          <el-input v-model="form.tradeCodeNorm" placeholder="请输入${comment}" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Mtplan">
import { listMtplan, getMtplan, delMtplan, addMtplan, updateMtplan } from "@/api/sbtz/mtplan";

const { proxy } = getCurrentInstance();

const mtplanList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    assetId: null,
    title: null,
    planDate: null,
    planContent: null,
    needSpare: null,
    tradeCode: null,
    priority: null,
    planType: null,
    windowStart: null,
    windowEnd: null,
    estDurationMin: null,
    assigneeGroup: null,
    assigneeUserId: null,
    cycleType: null,
    cycleValue: null,
    nextPlanDate: null,
    genPolicy: null,
    status: null,
    createdBy: null,
    deleted: null,
    tradeCodeNorm: null
  },
  rules: {
    assetId: [
      { required: true, message: "设备ID不能为空", trigger: "blur" }
    ],
    // title: [
    //   { required: true, message: "计划标题不能为空", trigger: "blur" }
    // ],
    // planDate: [
    //   { required: true, message: "计划维护日期不能为空", trigger: "blur" }
    // ],
    // planContent: [
    //   { required: true, message: "计划维护内容不能为空", trigger: "blur" }
    // ],
    // needSpare: [
    //   { required: true, message: "是否需要备件：0-否 1-是不能为空", trigger: "blur" }
    // ],
    // tradeCode: [
    //   { required: true, message: "计划维护工种代码不能为空", trigger: "blur" }
    // ],
    // priority: [
    //   { required: true, message: "优先级不能为空", trigger: "blur" }
    // ],
    // planType: [
    //   { required: true, message: "计划类型：保养/点检/修复不能为空", trigger: "change" }
    // ],
    // estDurationMin: [
    //   { required: true, message: "预估工时不能为空", trigger: "blur" }
    // ],
    // assigneeGroup: [
    //   { required: true, message: "目标班组不能为空", trigger: "blur" }
    // ],
    // cycleType: [
    //   { required: true, message: "周期类型不能为空", trigger: "change" }
    // ],
    // cycleValue: [
    //   { required: true, message: "周期数值，如每3月一次=&gt;MONTH,3不能为空", trigger: "blur" }
    // ],
    // genPolicy: [
    //   { required: true, message: "到期生成工单策略：手动/自动不能为空", trigger: "blur" }
    // ],
    // status: [
    //   { required: true, message: "计划状态不能为空", trigger: "change" }
    // ],
    // remark: [
    //   { required: true, message: "备注不能为空", trigger: "blur" }
    // ],
    // createdBy: [
    //   { required: true, message: "创建人不能为空", trigger: "blur" }
    // ],
    // createTime: [
    //   { required: true, message: "创建时间不能为空", trigger: "blur" }
    // ],
    // updateTime: [
    //   { required: true, message: "更新时间不能为空", trigger: "blur" }
    // ],
    // deleted: [
    //   { required: true, message: "逻辑删除：0-否 1-是不能为空", trigger: "blur" }
    // ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询设备计划维护列表 */
function getList() {
  loading.value = true;
  listMtplan(queryParams.value).then(response => {
    mtplanList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    assetId: null,
    title: null,
    planDate: null,
    planContent: null,
    needSpare: null,
    tradeCode: null,
    priority: null,
    planType: null,
    windowStart: null,
    windowEnd: null,
    estDurationMin: null,
    assigneeGroup: null,
    assigneeUserId: null,
    cycleType: null,
    cycleValue: null,
    nextPlanDate: null,
    genPolicy: null,
    status: null,
    remark: null,
    createdBy: null,
    createTime: null,
    updateTime: null,
    deleted: null,
    tradeCodeNorm: null
  };
  proxy.resetForm("mtplanRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加设备计划维护";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getMtplan(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改设备计划维护";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["mtplanRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateMtplan(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addMtplan(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除设备计划维护编号为"' + _ids + '"的数据项？').then(function() {
    return delMtplan(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('sbtz/mtplan/export', {
    ...queryParams.value
  }, `mtplan_${new Date().getTime()}.xlsx`)
}

getList();
</script>
